﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CreateBase.cs" company="SmartPlan">
//   SmartPlan 2011
// </copyright>
// <summary>
//   The create base.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DbDiff.Common.Scripting
{
    using System;
    using System.Data;
    using DataAccess;

    /// <summary>
    /// The create base.
    /// </summary>
    internal abstract class CreateBase : SqlString
    {
        /// <summary>
        /// The schema.
        /// </summary>
        protected readonly string Schema = string.Empty;

        /// <summary>
        /// The table name.
        /// </summary>
        protected readonly string TableName = string.Empty;

        /// <summary>
        /// The name of the object
        /// </summary>
        protected readonly string Name = string.Empty;

        /// <summary>
        /// The data row view
        /// </summary>
        protected DataRowView[] DataRowViews;

        /// <summary>
        /// The dt objects.
        /// </summary>
        protected readonly DatabaseDataSets DatabaseDiffData;

        /// <summary>
        /// The current row.
        /// </summary>
        protected int CurrentRow;

        /// <summary>
        /// The scr option.
        /// </summary>
        protected readonly SqlScriptingOption scrOption;

        /// <summary>
        /// The sql version.
        /// </summary>
        // protected SqlVersionInternal sqlVersion;

        /// <summary>
        /// Initializes a new instance of the <see cref="CreateBase"/> class.
        /// </summary>
        /// <param name="schema">
        /// The schema name.
        /// </param>
        /// <param name="tableName">
        /// The table name.
        /// </param>
        /// <param name="name">
        /// The name of th object.
        /// </param>
        /// <param name="dtObjects">
        /// The dt objects.
        /// </param>
        /// <param name="scrOption">
        /// The scr option.
        /// </param>
        /// <param name="sqlVersion">
        /// The sql version.
        /// </param>
        public CreateBase(string schema, string tableName, string name, DatabaseDataSets dtObjects, SqlScriptingOption scrOption)
        {
            this.Schema = schema;
            this.TableName = tableName;
            this.Name = name;
            this.DatabaseDiffData = dtObjects;
            this.SetRows();
            this.SetCurrentRow();
            this.CurrentRow = -1;
            this.scrOption = scrOption;
        }

        /// <summary>
        /// The to string.
        /// </summary>
        /// <returns>
        /// The script of the object
        /// </returns>
        public override string ToString()
        {
            if (!this.scrOption.Drop)
            {
                if (this.DataRowViews.Length != 0)
                {
                    while (this.NextRow() < this.DataRowViews.Length)
                    {
                        this.RowScript();
                    }
                }
            }
            else
            {
                this.RowScript();
            }

            return this.cmd.ToString();
        }

        /// <summary>
        /// The script.
        /// </summary>
        /// <returns>
        /// The script of the object
        /// </returns>
        public virtual string Script()
        {
            return this.ToString();
        }

        /// <summary>
        /// The set row method
        /// </summary>
        protected virtual void SetRows()
        {
        }

        /// <summary>
        /// The next row.
        /// </summary>
        /// <returns>
        /// Returns the next row.
        /// </returns>
        protected virtual int NextRow()
        {
            this.CurrentRow++;

            if (this.CurrentRow < this.DataRowViews.Length)
            {
                this.SetCurrentRow();
            }

            return this.CurrentRow;
        }

        /// <summary>
        /// Set the current row
        /// </summary>
        protected virtual void SetCurrentRow()
        {
        }

        /// <summary>
        /// The drop method script.
        /// </summary>
        /// <returns>
        /// The drop string of the object
        /// </returns>
        protected virtual string Drop()
        {
            return string.Empty;
        }

        /// <summary>
        /// The if exists.
        /// </summary>
        /// <returns>
        /// The if exists string for the object creation/alter decision
        /// </returns>
        protected virtual string IfExists()
        {
            return string.Empty;
        }

        /// <summary>
        /// Implement current row script
        /// </summary>
        protected virtual void RowScript()
        {
        }
    }
}
